package com.kc.sjwh.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.kc.sjwh.entity.bo.UserBo;
import com.kc.sjwh.entity.po.Zhzxpzgxhzbsj;
import com.kc.sjwh.entity.po.Zhzxpzxx;
import com.kc.sjwh.entity.vo.dict.DictItem;
import com.kc.sjwh.other.AjaxRes;
import com.kc.sjwh.other.Page;
import com.kc.sjwh.service.SysService;
import com.kc.sjwh.service.ZhzxpzxxService;

/**
 * 处置力量页action
 * @author xianding
 * @date 2017年11月28日
 */
@Controller
@RequestMapping("zhzxpzxx")
public class ZhzxpzxxAction extends BaseAction {
	static Logger logger = Logger.getLogger(ZhzxpzxxAction.class);
	
	@Autowired
	ZhzxpzxxService service;
	@Autowired
	SysService sysService;
	/**
	 * 打开列表页
	 * @param req
	 * @return
	 */
	@RequestMapping("list")
	public String list(HttpServletRequest req) {
		List<DictItem> sx=sysService.getDictByType("指挥中心属性");
		req.setAttribute("sx",sx);
		return "zhzxpzxx/zhzxpzxxList";
	}
	
	/**
	 * 分页查询
	 * @param req
	 * @param pageSize
	 * @param pageNum
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getPage")
	public String getPage(HttpServletRequest req, int pageSize, int pageNum) {
		Map<String, String> m = super.getParamMap(req);

		if(!SecurityUtils.getSubject().hasRole("admin")) {
			m.put("dwdm", getLoginUser().getSsdwdm());
		}
		
		Page<Zhzxpzxx> page = service.getPage(pageSize, pageNum, m);
//		
//		StringBuffer sb = new StringBuffer();
//		for(Zhzxpzxx one : page.getList()) {//数据行
//			ZhzxpzxxBo bo = (ZhzxpzxxBo)one;
//			sb.append("<tr>");
//			sb.append("<td>"+new SimpleDateFormat("yy/MM/dd HH:mm").format(bo.getCjsj())+"</td>");
//			sb.append("<td>"+chk(bo.getYbh())+"</td>");
//			sb.append("<td>"+chk(bo.getYmc())+"</td>");
//			sb.append("<td>"+chk(bo.getSsdwmc())+"</td>");
//			sb.append("<td>");
////			sb.append("	<a  data-toggle='modal' data-iframe='form?id="+bo.getYbh()+"' data-size='lg'>核查</a> &nbsp;");
//			sb.append("	<a href='form?id="+bo.getYbh()+"'>编辑</a> &nbsp;");
//			sb.append("	<a href='../czllz/form?ybh="+bo.getYbh()+"'>新增组</a> &nbsp;");
//			sb.append("	<a onclick=\"deletes('"+bo.getYbh()+"')\">删除</a>");
//			sb.append("</td>");
//			sb.append("</tr>");
//		}
//		
//		//返回结果
//		Map data = new HashMap();
//		data.put("total", page.getTotal());
//		data.put("html", sb.toString());
		return JSON.toJSONString(new AjaxRes(page));
	}
	
	/**
	 * 打开表单(新增或修改)
	 * @param id
	 */
	@RequestMapping("form")
	public String form(HttpServletRequest req, String id) {
		req.setAttribute("act", id==null?"add":"update");
		req.setAttribute("actName", id==null?"新增":"修改");
		req.setAttribute("one", service.form(id));
		req.setAttribute("loginUser", super.getLoginUser());
		return "zhzxpzxx/zhzxpzxxForm";
	}
	
	/**
	 * 保存（新增或修改）
	 * @param one
	 * @param act add/update
	 */
	@ResponseBody
	@RequestMapping("save")
	public String save(Zhzxpzxx one, String act) {
		if("add".equals(act)) {
			UserBo user = getLoginUser();
			one.setCjr(user.getMc());
			if(StringUtils.isEmpty(one.getSsdwdm())) {
				one.setSsdwdm(user.getSsdwdm());
				one.setSsdwmc(user.getSsdwmc());
			}
			service.add(one);
		} else {
			one.setGxr(getLoginUser().getMc());
			service.update(one);
		}
		return JSON.toJSONString(new AjaxRes());
	}
	
	/**
	 * 删除
	 * @param req
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delete")
	public String delete(HttpServletRequest req, String id) {
		service.delete(id);
		return JSON.toJSONString(new AjaxRes());
	}
	
	
	/**
	 * 获取模板
	 */
	@ResponseBody
	@RequestMapping("getModelByIp")
	public String getModelByIp(HttpServletRequest req) {
		String zbxbh=req.getParameter("zbxbh");
		String zhzxbh=req.getParameter("zhzxbh");
		String model=service.getModelByIp(zbxbh,zhzxbh);
		//返回结果
		Map data = new HashMap();
		data.put("model", model==null?"":model);
		return JSON.toJSONString(new AjaxRes(data));
	}
	/**
	 * 配置工作台(新增或修改)
	 * @param id
	 */
	@RequestMapping("cfg")
	public String conf(HttpServletRequest req, String id) {
		req.setAttribute("pzx", sysService.getAllPzxDict());
		req.setAttribute("one", service.form(id));
		req.setAttribute("loginUser", super.getLoginUser());
		return "zhzxpzxx/zhzxpzxxCfg";
	}

	/**
	 * 保存配置（新增或修改）
	 * @param one
	 * @param act add/update
	 */
	@ResponseBody
	@RequestMapping("saveCfg")
	public String saveCfg(Zhzxpzgxhzbsj one, String act) {
		Zhzxpzgxhzbsj up=service.getCfg(one);
		if(up==null) {
			UserBo user = getLoginUser();
			one.setCjr(user.getMc());
			service.addCfg(one);
		} else {
			one.setGxr(getLoginUser().getMc());
			service.updateCfg(one,up.getBh());
		}
		return JSON.toJSONString(new AjaxRes());
	}
	
}
